package org.zhaolei.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.zhaolei.request.UserLoginRequest;
import org.zhaolei.request.UserRegisterRequest;
import org.zhaolei.service.FileService;
import org.zhaolei.service.UserService;
import org.zhaolei.util.JsonData;
import org.zhaolei.enums.BizCodeEnum;
import org.zhaolei.vo.UserVO;

@Api(tags = "用户模块")
@RestController
@RequestMapping("/api/user/v1")
public class UserController {

    @Autowired
    private FileService fileService;

    @Autowired
    private UserService userService;


    @ApiOperation("用户头像上传")
    @PostMapping(value = "upload")
    public JsonData uploadUserImg(
            @ApiParam(value = "头像文件上传", required = true)
            @RequestPart("file") MultipartFile file) {

        String result = fileService.uploadUserImg(file);

        return result == null ? JsonData.buildResult(BizCodeEnum.FILE_UPLOAD_USER_IMG_FAIL) : JsonData.buildSuccess(result);
    }

    /**
     * 用户注册
     * @param userRegisterRequest
     * @return
     */
    @ApiOperation("用户注册")
    @PostMapping("register")
    public JsonData register(@ApiParam("用户注册对象") @RequestBody UserRegisterRequest userRegisterRequest) {
        JsonData result = userService.register(userRegisterRequest);
        return result;
    }

    /**
     * 用户登录
     * @return
     */
    @ApiOperation("用户登录")
    @PostMapping("login")
    public JsonData login(@ApiParam("用户登录对象") @RequestBody UserLoginRequest userLoginRequest) {
        JsonData jsonData = userService.login(userLoginRequest);
        return jsonData;
    }

    @ApiOperation("用户详情")
    @GetMapping("detail")
    public JsonData detail(@RequestParam Long userId) {
        //从数据库里拿到用户的单条数据之后，怎么构造返回数据？？需要在JsonData里面加map
        //因为UserDO里面有敏感字段，所以不能用UserDO，比如密码，盐等等
        UserVO userVO = userService.findUserDetail();

        return JsonData.buildSuccess(userVO);
    }
}
